package com.ruoyi.project.yingyu.activitycheckin.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.kkuser.domain.KKUser;
import com.ruoyi.project.user.kkuser.service.IKKUserService;
import com.ruoyi.project.yingyu.activitycheckin.domain.ActivityCheckin;
import com.ruoyi.project.yingyu.activitycheckin.service.IActivityCheckinService;

/**
 * 活动签到Controller
 * 
 * @author shixiangwen
 * @date 2020-03-13
 */
@Controller
@RequestMapping("/yingyu/activitycheckin")
public class ActivityCheckinController extends BaseController {
	private String prefix = "yingyu/activitycheckin";

	@Autowired
	private IActivityCheckinService activityCheckinService;

	@Autowired
	IKKUserService kkUserService;

	@RequiresPermissions("yingyu:activitycheckin:view")
	@GetMapping("/{activityId}/{kkid}")
	public String activitycheckin(@PathVariable("activityId") Integer activityId, @PathVariable("kkid") Long kkid, ModelMap mmap) {
		mmap.addAttribute("activityId", activityId);
		mmap.addAttribute("kkid", kkid);

		KKUser search = new KKUser();
		search.setKkid(kkid);
		search.setAppId("yingyuword");
		List<KKUser> users = kkUserService.selectKKUserList(search);
		if (!CollectionUtils.isEmpty(users)) {
			mmap.addAttribute("user", users.get(0));
		}

		return prefix + "/activitycheckin";

	}

	/**
	 * 查询活动签到列表
	 */
	@RequiresPermissions("yingyu:activitycheckin:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ActivityCheckin activityCheckin) {
		startPage();
		List<ActivityCheckin> list = activityCheckinService.selectActivityCheckinList(activityCheckin);
		return getDataTable(list);
	}

	/**
	 * 导出活动签到列表
	 */
	@RequiresPermissions("yingyu:activitycheckin:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ActivityCheckin activityCheckin) {
		List<ActivityCheckin> list = activityCheckinService.selectActivityCheckinList(activityCheckin);
		ExcelUtil<ActivityCheckin> util = new ExcelUtil<ActivityCheckin>(ActivityCheckin.class);
		return util.exportExcel(list, "activitycheckin");
	}

	/**
	 * 新增活动签到
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存活动签到
	 */
	@RequiresPermissions("yingyu:activitycheckin:add")
	@Log(title = "活动签到", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ActivityCheckin activityCheckin) {
		return toAjax(activityCheckinService.insertActivityCheckin(activityCheckin));
	}

	/**
	 * 修改活动签到
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		ActivityCheckin activityCheckin = activityCheckinService.selectActivityCheckinById(id);
		mmap.put("activityCheckin", activityCheckin);
		return prefix + "/edit";
	}

	/**
	 * 修改保存活动签到
	 */
	@RequiresPermissions("yingyu:activitycheckin:edit")
	@Log(title = "活动签到", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ActivityCheckin activityCheckin) {
		return toAjax(activityCheckinService.updateActivityCheckin(activityCheckin));
	}

	/**
	 * 删除活动签到
	 */
	@RequiresPermissions("yingyu:activitycheckin:remove")
	@Log(title = "活动签到", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(activityCheckinService.deleteActivityCheckinByIds(ids));
	}
}
